/*  ------------------------------------------------------------------------  */
/*  Paper        : Does Stakeholder Outrage Determine Executive Pay?          */
/*  Program      : a01-07-execucomp.do                                        */
/*  Description  : Creating Execucomp panel                                   */
/*  ------------------------------------------------------------------------  */

/*  ------------------------------------  */
//  Import Execucomp

import sas using "$comp/execcomp/anncomp.sas7bdat", clear

rename *, lower

gen _tdc1 = -(tdc1)
egen _tdc1Rank = rank(_tdc1), by(gvkey year) unique
tab _tdc1Rank
sort gvkey year _tdc1Rank
drop _tdc1
gen _top5 = (_tdc1Rank <= 5)
egen _tdc1Total = sum(tdc1), by(gvkey year _top5)
replace _tdc1Total = . if _top5==0
egen tdc1Total = max(_tdc1Total), by(gvkey year)
drop _tdc1Rank _top5 _tdc1Total

keep if ceoann == "CEO"
egen gvkey_exec = group(gvkey)
order gvkey_exec, after(gvkey)
sort execid
egen exec_id = group(execid)

by gvkey_exec year, sort: egen _cnt = count(gvkey_exec)
gen _miss_became = missing(becameceo)
by gvkey_exec year, sort: egen _sum_became = sum(_miss_became)
gen _miss_leftofc = missing(leftofc)
by gvkey_exec year, sort: egen _sum_leftofc = sum(_miss_leftofc)

/*  ------------------------------------  */
//  If two CEOs in the same year, keep the one with non-missing dates

sort gvkey year becameceo leftofc
by gvkey year: gen _seq = _n
drop if _cnt==2 & missing(becameceo) & missing(leftofc) & _seq!=1
drop _seq _cnt
by gvkey_exec year, sort: egen _cnt = count(gvkey_exec)
sort gvkey year leftofc
by gvkey year: gen _seq = _n

/*  ------------------------------------  */
//  Remove CEO with earlier or non-missing leave date

tab _seq
drop if _cnt==2 & _seq ==1 
ds _*
drop _*

gen varcomp = tdc1 - salary

/*  ------------------------------------  */
//  Set panel
xtset gvkey_exec year

#delimit ;
local compvars
	tdc1 salary varcomp bonus stock_awards_fv option_awards_fv
	noneq_incent othcomp defer_rpt_as_comp_tot tdc1Total
; #delimit cr

local basevars gvkey gvkey_exec exec_id year execid exec_fullname becameceo

keep `basevars' `compvars' sal_pct shrown_tot_pct titleann

/*  ------------------------------------  */
//  Update compensation variables
//  Generate change and fractions

#delimit ;
local compvars tdc1 salary varcomp bonus stock_awards_fv option_awards_fv
	noneq_incent othcomp defer_rpt_as_comp_tot tdc1Total
; #delimit cr

foreach yvar in `compvars' {
di "---------------------"
di _newline "`yvar'"
di _newline "Set missing to zero"
replace `yvar' = 0 if missing(`yvar')
di "---------------------"
di _newline
sort gvkey_exec year becameceo
capture drop `yvar'_chg
gen `yvar'_chg = (`yvar' / l1.`yvar')-1
replace `yvar'_chg = 0 if missing(`yvar'_chg)
replace `yvar'_chg = . if exec_id != l1.exec_id

capture drop `yvar'_fr
	gen `yvar'_fr = `yvar' / tdc1
	replace `yvar'_fr = 0 if missing(`yvar'_fr) & tdc1==0
}

/*  ------------------------------------  */
//  Update compensation variables
//  Log of compensation components

#delimit ;
global compvars
	tdc1 salary varcomp bonus stock_awards_fv option_awards_fv
	noneq_incent othcomp defer_rpt_as_comp_tot
; #delimit cr

foreach yvar in $compvars shrown_tot_pct {

	capture drop ln_`yvar'
	di "Processing `yvar' ..."
	qui summ `yvar'
	gen ln_`yvar' = ln(abs(`r(min)') + 1+`yvar')
	order ln_`yvar', after(`yvar')
	di "Processing `yvar' ... done." _newline
// 	histogram ln_`yvar' if fyear==2019, name(ln_`yvar', replace)
	}



/*  ------------------------------------  */
//  CEO Tenure

egen firmceo_fe = group(gvkey exec_id)
xtset firmceo_fe year
by firmceo_fe, sort: gen ceo_tenure = _n
tab ceo_tenure
	xtset, clear
	drop firmceo_fe

/*  ------------------------------------  */
//  CEO duality 

// br if regexm(lower(titleann),"chair")
gen dchair = 1 if regexm(lower(titleann),"chair")
	replace dchair=0 if missing(dchair)
gen duality = (dchair ==1)

drop gvkey_exec titleann

gen tdc1_cps = tdc1 / tdc1Total
order tdc1_cps, after(tdc1)
drop tdc1Total

save "$temp112/compensation.dta", replace

/*  -|----------------------------------------------------------------------  */
/*  ||  ---    Program end: a01-07-execucomp.do                               */
/*  -|  ---    Attila Balogh    --------------------------------------------  */